//
//  PullMenuView.swift
//  Employee
//
//  Created by swordjoy on 2017/6/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class MoreButton: UIView {
    
  var clickClosure: VoidClosure?
  
  var title: String = "" {
    didSet {
      titleLabel.text = title
    }
  }
    
    var titleColor: UIColor = GlobalConfig.TextLightColor {
        didSet {
            titleLabel.textColor = titleColor
        }
    }
  
  var isMore: Bool = false {
    didSet {
      if isMore {
        moreImageView.image = UIImage(named: "func_more_on")
      }
      else {
        moreImageView.image = UIImage(named: "func_more_off")
      }
    }
  }
  
  fileprivate var titleLabel: UILabel!
  fileprivate var moreImageView: UIImageView!
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.white
    
    titleLabel = UILabel()
    addSubview(titleLabel)
    titleLabel.textColor = GlobalConfig.TextLightColor
    titleLabel.font = GlobalConfig.system14Font
    titleLabel.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.centerY.equalToSuperview()
      make.width.equalTo(58)
    }
    
    moreImageView = UIImageView()
    addSubview(moreImageView)
    moreImageView.image = UIImage(named: "func_more_off")
    moreImageView.snp.makeConstraints { (make) in
      make.centerY.equalToSuperview()
      make.left.equalTo(titleLabel.snp.right).offset(5)
    }
    
    let tapGesure = UITapGestureRecognizer(target: self, action: #selector(moreAction))
    addGestureRecognizer(tapGesure)
  }
  
  @objc fileprivate func moreAction() {
    clickClosure?()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  
}

enum PullMenuStateEnum {
  case all
  case checking
  case finish
  case pass
  case recall
}

protocol PullMenuViewDelegate: class {
  func pullMenuStateChange(index: PullMenuStateEnum)
  func pullMenuTypeChange(typeId: String)
    func pullMenuDefault()
}

class PullMenuView: UIView {

  var type: [String] = [] {
    didSet {
      
    }
  }
  
  weak var delegate: PullMenuViewDelegate?
  
  var flows: [Flow] = []
  
  fileprivate var backgroundView: UIView!
  fileprivate var tableView: UITableView!
  
  fileprivate var barContainerView: UIView!
  fileprivate var stateButton: MoreButton!
  fileprivate var typeButton: MoreButton!
//  fileprivate var lastState: Int = 1
  fileprivate var lastType: Int = 0
  
  fileprivate var states: [String] = []
  fileprivate var isMore: Bool = false
    
    fileprivate var index: Int = 0
    fileprivate var lastState: Int = 0
  
  // 0是状态,1是类型
  fileprivate var flag: Int = 0
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.clear
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - interface methods

extension PullMenuView {
    
    func updateStates(_ index: Int) {
        dismiss()
        
        if 0 == index {
            lastState = 0
            states = ["全部状态", "审批中", "已完成", "未通过", "已撤回"]
            stateButton.title = "全部状态"
            let indexPath = IndexPath(row: 0, section: 0)
            tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
        }
        else {
            states = ["全部状态", "审批中", "已完成", "未通过"]
            if 1 == index {
                lastState = 0
                stateButton.title = "全部状态"
                let indexPath = IndexPath(row: 0, section: 0)
                tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
            }
            else if 2 == index {
                lastState = 0
                stateButton.title = "全部状态"
                let indexPath = IndexPath(row: 0, section: 0)
                tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
            }
        }
        lastType = 0
        typeButton.title = "全部类型"
    }
    
}

// MARK: - action

extension PullMenuView {
  
  fileprivate func show() {
    isMore = true
    sj.height = UIScreen.height - 108
    self.tableView.reloadData()
    backgroundView.alpha = 0
    tableView.sj.height = 0
    if 0 == flag {
        stateButton.titleColor = GlobalConfig.BGSkyBlueColor
        typeButton.titleColor = GlobalConfig.TextLightColor
    }
    else {
        typeButton.titleColor = GlobalConfig.BGSkyBlueColor
        stateButton.titleColor = GlobalConfig.TextLightColor
    }
    addSubview(backgroundView)
    addSubview(tableView)
    UIView.animate(withDuration: 0.1) {
      self.backgroundView.alpha = 1
      self.tableView.sj.height = UIScreen.height - 152
    }
  }
  
  @objc fileprivate func dismiss() {
    isMore = false
    stateButton.isMore = false
    typeButton.isMore = false
      stateButton.titleColor = GlobalConfig.TextLightColor
      typeButton.titleColor = GlobalConfig.TextLightColor
    sj.height = 44
    UIView.animate(withDuration: 0.1, animations: {
      self.backgroundView.alpha = 0
      self.tableView.sj.height = 0
    }) { (finish) in
      self.backgroundView.removeFromSuperview()
      self.tableView.removeFromSuperview()
    }
  }
  
}

// MARK: - UITableViewDelegate

extension PullMenuView: UITableViewDelegate {
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    lastState = indexPath.row
    if 0 == flag {
      switch indexPath.row {
      case 0:
        delegate?.pullMenuStateChange(index: .all)
        stateButton.title = "全部状态"
      case 1:
        delegate?.pullMenuStateChange(index: .checking)
        stateButton.title = "审批中"
      case 2:
        delegate?.pullMenuStateChange(index: .finish)
        stateButton.title = "已完成"
      case 3:
        delegate?.pullMenuStateChange(index: .pass)
        stateButton.title = "未通过"
      case 4:
        delegate?.pullMenuStateChange(index: .recall)
        stateButton.title = "已撤回"
      default:
        break
      }
      dismiss()
      stateButton.isMore = false
    }
    else {
      lastType = indexPath.row
      delegate?.pullMenuTypeChange(typeId: flows[indexPath.row].id)
      if 0 == indexPath.row { typeButton.title = "全部类型" }
      else { typeButton.title = flows[indexPath.row].name }
      dismiss()
      typeButton.isMore = false
    }
  }
}

// MARK: - UITableViewDataSource

extension PullMenuView: UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if 0 == flag { return states.count }
    else { return flows.count }
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: PullCell.identifiler) as! PullCell
    
    if 0 == flag { cell.title = states[indexPath.row] }
    else { cell.title = flows[indexPath.row].name }
    
    return cell
  }
}

// MARK: - fileprivate

extension PullMenuView {
  
  fileprivate func create() {
    states = ["全部", "审批中", "已完成", "未通过", "已撤回"]
    
    barContainerView = UIView()
    addSubview(barContainerView)
    barContainerView.backgroundColor = UIColor.white
    barContainerView.snp.makeConstraints { (make) in
      make.top.right.left.equalToSuperview()
      make.height.equalTo(44)
    }
    
    stateButton = MoreButton(frame: CGRect(x: 0, y: 0, width: 92, height: 44))
    stateButton.title = "审批中"
    barContainerView.addSubview(stateButton)
    
    typeButton = MoreButton(frame: CGRect(x: 92, y: 0, width: 92, height: 44))
    typeButton.title = "全部类型"
    barContainerView.addSubview(typeButton)
    
    backgroundView = UIView(frame: CGRect(x: 0, y: 44, width: UIScreen.width, height: UIScreen.height - 152))
    backgroundView.backgroundColor = UIColor(white: 0, alpha: 0.3)
    
    
    tableView = UITableView(frame: CGRect(x: 0, y: 44, width: UIScreen.width, height: UIScreen.height - 152))
    tableView.backgroundColor = UIColor.clear
    tableView.delegate = self
    tableView.dataSource = self
    tableView.separatorStyle = .none
    tableView.bounces = false
    tableView.rowHeight = 46
    tableView.register(PullCell.self, forCellReuseIdentifier: PullCell.identifiler)
    
    stateButton.clickClosure = { [unowned self] in
      self.stateButton.isMore = !self.stateButton.isMore
      if self.isMore && 1 != self.flag {
        self.dismiss()
      }
      else {
        self.typeButton.isMore = false
        self.flag = 0
        
        let indexPath = IndexPath(row: self.lastState, section: 0)
        
        self.show()
        self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
      }
    }
    
    typeButton.clickClosure = { [unowned self] in
      self.typeButton.isMore = !self.typeButton.isMore
      if self.isMore && 0 != self.flag {
        self.dismiss()
      }
      else {
        self.stateButton.isMore = false
        self.flag = 1
        let indexPath = IndexPath(row: self.lastType, section: 0)
        self.show()
        self.tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
      }
    }
    
  }
  
}
